﻿package mortal.game.control
{
    import Message.Public.*;
    import extend.language.*;
    import flash.events.*;
    import mortal.common.error.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.proxy.*;
    import mortal.game.view.business.*;
    import mortal.game.view.copy.QuestionCopy.data.*;
    import mortal.game.view.systemSetting.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class TradeController extends Controller
    {
        private var _tradeCache:TradeCache;
        private var _tradeProxy:TradeProxy;
        private var _tradeModule:TradeModule;

        public function TradeController()
        {
            this.init();
            return;
        }// end function

        private function init() : void
        {
            this._tradeCache = cache.trade;
            this._tradeProxy = GameProxy.trade;
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._tradeModule == null)
            {
                this._tradeModule = new TradeModule();
                this._tradeModule.addEventListener(WindowEvent.SHOW, this.onMarketShow);
                this._tradeModule.addEventListener(WindowEvent.CLOSE, this.onMarketClose);
            }
            return this._tradeModule;
        }// end function

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.TradeAcceptTrade, this.selfAcceptTrade);
            Dispatcher.addEventListener(EventName.TradeRejectTrade, this.selfRejectTrade);
            Dispatcher.addEventListener(EventName.TradeApplyToTarget, this.onApply);
            NetDispatcher.addCmdListener(ServerCommand.TradeUpdate, this.onTradeMsg);
            return;
        }// end function

        protected function onMarketShow(event:Event) : void
        {
            Dispatcher.addEventListener(EventName.TradeMyItemsChange, this.onMyTradeItemsChange);
            Dispatcher.addEventListener(EventName.TradeMyMoneysChange, this.onMyTradeMoneysChange);
            Dispatcher.addEventListener(EventName.TradeClickLock, this.onClickLock);
            Dispatcher.addEventListener(EventName.TradeClickTradeBtn, this.onClickTradeBtn);
            Dispatcher.addEventListener(EventName.TradeCancel, this.onClickTradeCancel);
            return;
        }// end function

        protected function onMarketClose(event:Event) : void
        {
            Dispatcher.removeEventListener(EventName.TradeMyItemsChange, this.onMyTradeItemsChange);
            Dispatcher.removeEventListener(EventName.TradeMyMoneysChange, this.onMyTradeMoneysChange);
            Dispatcher.removeEventListener(EventName.TradeClickLock, this.onClickLock);
            Dispatcher.removeEventListener(EventName.TradeClickTradeBtn, this.onClickTradeBtn);
            Dispatcher.removeEventListener(EventName.TradeCancel, this.onClickTradeCancel);
            return;
        }// end function

        private function onMyTradeMoneysChange(event:DataEvent) : void
        {
            this._tradeProxy.updateMoney(this._tradeCache.currentBusinessId, EPriceUnit.convert(event.data.unit), event.data.amount);
            return;
        }// end function

        private function onMyTradeItemsChange(event:DataEvent) : void
        {
            var _loc_2:* = event.data.amount;
            var _loc_3:* = event.data.uid;
            if (_loc_2 > 0)
            {
                this._tradeCache.usedItems[_loc_3] = _loc_2;
            }
            else
            {
                delete this._tradeCache.usedItems[_loc_3];
            }
            this._tradeProxy.updateItem(this._tradeCache.currentBusinessId, _loc_3, _loc_2);
            Dispatcher.dispatchEvent(new DataEvent(EventName.Trade_StatusChange));
            return;
        }// end function

        private function onApply(event:DataEvent) : void
        {
            if (!LevelCheckUtil.isLevelEnougth(LevelCheckUtil.trade))
            {
                MsgManager.showRollTipsMsg(Language.getString(20437));
                return;
            }
            this._tradeProxy.doApply(event.data as SEntityId);
            return;
        }// end function

        private function selfRejectTrade(event:DataEvent) : void
        {
            this._tradeProxy.doOperation(EBusinessOperation._EBusinessOperationReject, event.data as String);
            return;
        }// end function

        private function selfAcceptTrade(event:DataEvent) : void
        {
            if (!LevelCheckUtil.isLevelEnougth(LevelCheckUtil.trade))
            {
                MsgManager.showRollTipsMsg(Language.getString(20437));
                return;
            }
            this._tradeProxy.doOperation(EBusinessOperation._EBusinessOperationAgree, event.data as String);
            return;
        }// end function

        private function onClickTradeBtn(event:DataEvent) : void
        {
            this._tradeProxy.doOperation(EBusinessOperation._EBusinessOperationConfirm, this._tradeCache.currentBusinessId);
            return;
        }// end function

        private function onClickLock(event:DataEvent) : void
        {
            this._tradeProxy.doOperation(EBusinessOperation._EBusinessOperationLock, this._tradeCache.currentBusinessId);
            return;
        }// end function

        private function onClickTradeCancel(event:DataEvent) : void
        {
            this._tradeProxy.doOperation(EBusinessOperation._EBusinessOperationCancel, this._tradeCache.currentBusinessId);
            this.clearAtTradeEnd();
            return;
        }// end function

        private function onTradeMsg(param1:Object) : void
        {
            var _loc_2:* = param1.messageBase as SBusiness;
            var _loc_3:* = this._tradeCache.getTargetSBusinessInfo(_loc_2);
            switch(_loc_2.operation.__value)
            {
                case EBusinessOperation._EBusinessOperationUpdateItem:
                {
                    this._tradeModule.lock1 = false;
                    this._tradeModule.lock2 = false;
                    this._tradeModule.updateTargetItem(_loc_3.items);
                    break;
                }
                case EBusinessOperation._EBusinessOperationUpdateMoney:
                {
                    this._tradeModule.lock1 = false;
                    this._tradeModule.lock2 = false;
                    this._tradeModule.updateTargetMoneys(_loc_3.gold, _loc_3.coin);
                    break;
                }
                case EBusinessOperation._EBusinessOperationLock:
                {
                    this._tradeModule.lock1 = true;
                    break;
                }
                case EBusinessOperation._EBusinessOperationConfirm:
                {
                    this.onTargetTradeComfirm(_loc_2);
                    break;
                }
                case EBusinessOperation._EBusinessOperationCancel:
                {
                    this.onTargetTradeCancel(_loc_2);
                    break;
                }
                case EBusinessOperation._EBusinessOperationEnd:
                {
                    this.onTradeEnd(_loc_2);
                    break;
                }
                case EBusinessOperation._EBusinessOperationAgree:
                {
                    this.onAgreeToTrade(_loc_2);
                    break;
                }
                case EBusinessOperation._EBusinessOperationApply:
                {
                    this.onTradeApply(_loc_2, _loc_3);
                    break;
                }
                case EBusinessOperation._EBusinessOperationError:
                {
                    MsgManager.showRollTipsMsg(ErrorCode.getErrorStringByCode(_loc_2.error.code));
                    break;
                }
                case EBusinessOperation._EBusinessOperationReject:
                {
                    MsgManager.showRollTipsMsg(Language.getStringByParam(20438, _loc_2.fromInfo.name));
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function onTargetTradeComfirm(param1:SBusiness) : void
        {
            return;
        }// end function

        private function onTargetTradeCancel(param1:SBusiness) : void
        {
            this.clearAtTradeEnd();
            MsgManager.showRollTipsMsg(Language.getString(20439));
            return;
        }// end function

        private function onTradeEnd(param1:SBusiness) : void
        {
            this.clearAtTradeEnd();
            MsgManager.showRollTipsMsg(Language.getString(20440));
            return;
        }// end function

        private function clearAtTradeEnd() : void
        {
            if (GameController.pack.isViewShow)
            {
                GameController.pack.view.hide();
            }
            if (this.isViewShow)
            {
                this.view.hide();
            }
            this._tradeCache.isTrading = false;
            Dispatcher.dispatchEvent(new DataEvent(EventName.Trade_StatusChange));
            return;
        }// end function

        private function onTradeApply(param1:SBusiness, param2:SBusinessInfo) : void
        {
            if (SystemSetting.instance.isRefuseTrade.bValue || cache.friend.isBlackList(param2.entityId))
            {
                this._tradeProxy.doOperation(EBusinessOperation._EBusinessOperationReject, param1.businessId);
                return;
            }
            this._tradeCache.addToRequest(param1);
            TradeIcon.instance.show();
            return;
        }// end function

        private function onAgreeToTrade(param1:SBusiness) : void
        {
            this._tradeCache.currentSbusiness = param1;
            LayerManager.windowLayer.tweenCenterWindow(this.view, GameController.pack.view);
            cache.trade.isTrading = true;
            Dispatcher.dispatchEvent(new DataEvent(EventName.Trade_StatusChange));
            return;
        }// end function

    }
}
